﻿
namespace MyFurion.Application.Controller
{
    /// <summary>
    /// 菜单
    /// </summary>
    public class MenuController:IDynamicApiController
    {
        private readonly MenuRepository _repository;
        private readonly RoleMenuRepository _roleMenuRepository;
        public MenuController(MenuRepository userRepository, RoleMenuRepository roleMenuRepository)
        {
            _repository = userRepository;
            _roleMenuRepository= roleMenuRepository;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task Add(MenuInfoAdd input)
        {
            if (!Enum.IsDefined(typeof(EnumMenuType), input.MenuType))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "菜单类别"));
            }
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            var model = input.Adapt<MenuInfo>();
            bool result=await _repository.Add(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task Update(MenuInfoUpdate input)
        {
            if (!Enum.IsDefined(typeof(EnumMenuType), input.MenuType))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "菜单类别"));
            }
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            var model =await _repository.GetEntityById(input.Id);
            if (model == null)
            {
                throw Oops.Oh(ErrorMsgConst.NoRecord);
            }
            model.ParentId = input.ParentId;
            model.MenuCode = input.MenuCode;
            model.MenuName= input.MenuName;
            model.MenuType = input.MenuType;
            model.MenuUrl = input.MenuUrl;
            model.MenuIcon= input.MenuIcon;
            model.Status= input.Status;
            model.SortNum = input.SortNum;
            model.Remark= input.Remark;
            bool result = await _repository.Update(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("delete")]
        public async Task Delete([FromQuery]BaseIdInput input)
        {
            bool isExists = await _repository.IsExists(it=>it.ParentId==input.Id);
            if (isExists)
            {
                throw Oops.Oh("存在下级菜单");
            }
            bool result = await _repository.DeleteById(input.Id);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
            else
            {
                //角色菜单权限删除
                await _roleMenuRepository.DeleteByWhere(it=>it.MenuId==input.Id);
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("batchDelete")]
        public async Task BatchDelete([FromQuery]BaseIdsInput input)
        {
            bool isExists = await _repository.IsExists(it =>input.Ids.Contains(it.ParentId.Value));
            if (isExists)
            {
                throw Oops.Oh("存在有下级菜单的数据");
            }
            bool result = await _repository.DeleteByWhere(it=>input.Ids.Contains(it.Id));
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
            else
            {
                //角色菜单权限删除
                await _roleMenuRepository.DeleteByWhere(it =>input.Ids.Contains(it.MenuId));
            }
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("setStatus")]
        public async Task ModifyStatus(SetStatusInfo input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool result = await _repository.Update(it => it.Id == input.Id, it => new MenuInfo() { Status = input.Status });
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.StatusError);
            }
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("page")]
        public async Task<PageResult<MenuInfo>>QueryPage(MenuInfoPage input)
        {
            return await _repository.GetPageList(input);
        }
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<List<MenuInfo>> QueryList(MenuInfoPage input)
        {
            return await _repository.GetDataList(input);
        }
        /// <summary>
        /// 根据id获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<MenuInfo> GetDetail(long id)
        {
            return await _repository.GetEntityById(id);
        }
        /// <summary>
        /// 获取全部树数据
        /// </summary>
        /// <returns></returns>
        [HttpPost("tree")]
        public async Task<List<MenuInfo>> QueryTree()
        {
            return await _repository.GetTreeData();
        }
        /// <summary>
        /// 根据条件获取树数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("treeData")]
        public async Task<List<MenuInfo>> QueryTree(MenuInfoPage input)
        {
            return await _repository.GetTree(input);
        }
        /// <summary>
        /// 根据父级id获取下级数据
        /// </summary>
        /// <param name="parentId">父级id</param>
        /// <returns></returns>
        [HttpGet("getByParent")]
        public async Task<List<MenuInfo>> GetByParentId(long parentId)
        {
            return await _repository.GetByParentId(parentId);
        }
    }
}
